package sysAuth

import grails.converters.JSON
import initConfig.SysInitData
import sysUtil.PageUtil
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdInterface.XdControllerInterface

class SysRoleController implements XdControllerInterface{

    def authUtilsService
    def index() {query()}

    @Override
    def delete() {
        del()
    }

    @Override
    def detail() {
        return null
    }

    @Override
    def del() {
        return null
    }

    @Override
    def edit() {
        return null
    }

    @Override
    def save() {
        return null
    }

    @Override
    def update() {
        def result = ResultJson.resultMap()
        try {
            def id = params.id
            def field = params?.field
            def value = params?.value

            SysRoles sysRoles = SysRoles.get(Integer.parseInt(id))
            if(sysRoles){
                if( SysInitData.AdminRole.keySet().contains(sysRoles.roleNo) ){
                    result.put(ResultJson.CODE.name,ResultCode.OPT_SYS_AUTH.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SYS_AUTH.message)
                }else {
                    sysRoles.setProperty(SysCommUtil.underlineToCamel(field),value)
                    sysRoles.save(flush: true)
                    result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
                    result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
                }
            }else {
                result.put(ResultJson.CODE.name,ResultCode.NOT_EXISTS_ERROR.code)
                result.put(ResultJson.MSG.name,ResultCode.NOT_EXISTS_ERROR.message)
            }
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result as JSON
        }
    }

    @Override
    def add() {

        def result = ResultJson.resultMap()
        try {
            def roleName = params.roleName?:'New Role Name'
            def roleType = params.roleType?:"FUNC_ROLE"

            new SysRoles(roleName: roleName,roleNo: UUID.randomUUID(),roleType:roleType,creator: params.userNo).save(flush: true)

            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)

        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
        }finally{
            render  result as JSON
        }
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def query() {
        def result = ResultJson.resultMap()
        try {
            def list = authUtilsService.listRoleListData(params)
//            println list
            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            result.put(ResultJson.DATA.name,list)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result as JSON
        }
    }

    @Override
    def getOne() {
        return null
    }

    /**
     * 更新角色-数据权限
     */
    def updateRoleData(){
        // println SysCommUtil.listParentByChild(SysOrganization.get(8),"parentId")
        def result = ResultJson.resultMap()
        try {
            def list = authUtilsService.updateRoleData(params)

            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            //result.put(ResultJson.DATA.name,list)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result as JSON
        }
    }

    /**
     * 获取用户拥有的角色清单，用于用户权限分配功能
     * @return
     */
    def userRoleList(){
        def result = ResultJson.resultMap()
        try {
            def list = authUtilsService.listRoleListData(params)

            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.OPT_SUCCESS.message)
            result.put(ResultJson.DATA.name,PageUtil.roleCheckboxHtml(list))
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result as JSON
        }
    }

    /**
     * 新增用户-角色
     */
    def addSysUserRole(){
        def result = ResultJson.resultMap()
        try {
            def flag = authUtilsService.addSysUserRole(params)
            result.put(ResultJson.CODE.name,flag.code)
            result.put(ResultJson.MSG.name,flag.msg)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,ResultCode.QUERY_ERROR.message)
            e.printStackTrace()
        }finally{
            render  result as JSON
        }
    }
}
